package com.qeegoo.b2bautozityp.moudle.good.viewmodel;

import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.good.model.CarModelBean;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsPropertyBean;
import com.qeegoo.b2bautozityp.moudle.good.view.GoodsActivity;
import com.qeegoo.b2bautozityp.moudle.good.view.GoodsPropertyFragment;
import com.qeegoo.b2bautozityp.moudle.good.view.brand.GoodsBrandActivity;
import com.qeegoo.b2bautozityp.moudle.good.view.carmodel.CarModelMainFragment;
import com.qeegoo.b2bautozityp.moudle.good.view.category.GoodsCategoryActivity;
import com.qeegoo.b2bautozityp.moudle.goods.model.GoodsDetailResult;

import java.util.ArrayList;

import base.lib.util.ActivityManager;
import base.lib.util.NavigateUtils;
import base.lib.util.ToastUtils;

/**
 * 王少岩 在 2017/9/15 创建了它
 */

public class GoodsInfoItemViewModel {
    public ObservableField<String> infoText = new ObservableField<>("");
    public ObservableField<String> infoHint = new ObservableField<>("");
    public ObservableField<String> labelText = new ObservableField<>("");
    public ObservableField<String> code = new ObservableField<>("");
    public ObservableField<Integer> iconLeft = new ObservableField<>(0);
    public ObservableField<Integer> iconRight = new ObservableField<>(0);
    public ObservableField<Boolean> selectable = new ObservableField<>(false);
    public ArrayList<GoodsPropertyBean.GoodsProperty> mProperties;
    private GoodsInfoViewModel mInfoViewModel;

    public GoodsInfoItemViewModel(GoodsInfoViewModel infoViewModel, int iconLeft, int iconRight, String labelText, String infoText, String infoHint, boolean selectable) {
        mInfoViewModel = infoViewModel;
        this.iconLeft.set(iconLeft);
        this.iconRight.set(iconRight);
        this.labelText.set(labelText);
        this.infoText.set(infoText);
        this.code.set("");
        this.infoHint.set(infoHint);
        this.selectable.set(selectable);
    }

    public GoodsInfoItemViewModel(GoodsInfoViewModel infoViewModel, int iconLeft, int iconRight, String labelText, String infoText, String code, String infoHint, boolean selectable) {
        mInfoViewModel = infoViewModel;
        this.iconLeft.set(iconLeft);
        this.iconRight.set(iconRight);
        this.labelText.set(labelText);
        this.infoText.set(infoText);
        this.code.set(code);
        this.infoHint.set(infoHint);
        this.selectable.set(selectable);
    }

    public GoodsInfoItemViewModel(GoodsInfoViewModel infoViewModel, int iconLeft, int iconRight, String labelText, String infoText, String code, String infoHint, boolean selectable, ArrayList<GoodsPropertyBean.GoodsProperty> properties) {
        mInfoViewModel = infoViewModel;
        this.iconLeft.set(iconLeft);
        this.iconRight.set(iconRight);
        this.labelText.set(labelText);
        this.infoText.set(infoText);
        this.code.set(code);
        this.infoHint.set(infoHint);
        this.selectable.set(selectable);
        this.mProperties = properties;
    }

    public static ObservableList<GoodsInfoItemViewModel> getList(GoodsInfoViewModel infoViewModel, GoodsDetailResult goodsDetailResult, GoodsPropertyBean goodsPropertyBean) {
        ObservableList<GoodsInfoItemViewModel> items_info = new ObservableArrayList<>();
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, R.mipmap.ic_arrow_right,
                "配件品质", goodsDetailResult.goods.productTypeText, goodsDetailResult.goods.productType, "请选择配件品质", true, goodsPropertyBean.productTypeList));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, goodsDetailResult.goods.goodsType.equals("2") ? 0 : R.mipmap.star, 0,
                "OE", goodsDetailResult.goods.oemCode, "请填写OE号", false));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, 0,
                "配件名称", goodsDetailResult.goods.name, "请填写配件名称", false));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, 0,
                "库存量", goodsDetailResult.goods.quantity, "请填写库存量", false));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, R.mipmap.ic_arrow_right,
                "发货仓库", goodsDetailResult.goods.warehouseText, goodsDetailResult.goods.warehouseId, "请选择发货仓库", true));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, R.mipmap.ic_arrow_right,
                "绑定分类", goodsDetailResult.goods.capacity, goodsDetailResult.goods.categoryId, "请选择分类", true));
        if (goodsDetailResult.goods.goodsType.equals("2")) {//拆车件
            items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, R.mipmap.ic_arrow_right,
                    "配件成色", goodsDetailResult.goods.productQuality, goodsDetailResult.goods.productQuality, "请选择成色", true, goodsPropertyBean.productQualityList));
        } else {
            items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, R.mipmap.ic_arrow_right,
                    "配件品牌", goodsDetailResult.goods.brandName, goodsDetailResult.goods.brandId, "请选择品牌", true));
        }
        StringBuilder sb_carModelId = new StringBuilder();
        String carModelId = "";
        int size = 0;
        if (goodsDetailResult.carSeries != null && goodsDetailResult.carSeries.size() > 0) {
            size = goodsDetailResult.carSeries.size();
            for (CarModelBean bean : goodsDetailResult.carSeries) {
                sb_carModelId.append(bean.id).append(",");
            }
        }
        if (sb_carModelId.length() > 0) {
            carModelId = sb_carModelId.substring(0, sb_carModelId.length() - 1);
        }
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, R.mipmap.star, R.mipmap.ic_arrow_right,
                "适用车型", "已选择" + size + "款车型", carModelId, "请选择适用车型", true));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, 0, 0,
                "商品备注", goodsDetailResult.goods.goodsRemark, "请填写商品备注", false));
        items_info.add(new GoodsInfoItemViewModel(infoViewModel, 0, R.mipmap.ic_arrow_right,
                "单位", goodsDetailResult.goods.goodsUnit, goodsDetailResult.goods.goodsUnitId, "请选择单位", true, goodsPropertyBean.goodsUnitList));
        if (!goodsDetailResult.goods.goodsType.equals("2")) {
            items_info.add(new GoodsInfoItemViewModel(infoViewModel, 0, 0,
                    "规格型号", goodsDetailResult.goods.goodsStyle, "请输入规格型号", false));
            items_info.add(new GoodsInfoItemViewModel(infoViewModel, 0, 0,
                    "出厂编号", goodsDetailResult.goods.serialNumber, "请输入出厂编号", false));
        }
        return items_info;
    }

    public ReplyCommand command = new ReplyCommand(() -> {
        switch (labelText.get()) {
            case "配件品质":
            case "配件成色":
            case "单位":
            case "发货仓库":
                ((GoodsActivity) ActivityManager.getActivity()).openGoodsPropertyFrag(GoodsPropertyFragment.newInstance(infoHint.get().replace("请", ""), labelText.get(), infoText.get(), mProperties));
                break;
            case "绑定分类":
                NavigateUtils.startActivityForResult(GoodsCategoryActivity.class, 1000);
                break;
            case "配件品牌":
                NavigateUtils.startActivityForResult(GoodsBrandActivity.class, 1000);
                break;
            case "适用车型":
                if (mInfoViewModel.commonPic.get() == R.mipmap.ic_switch_open) {
                    ToastUtils.showToast("该商品已经设置为通用件，无需选择车型");
                } else {
                    ((GoodsActivity) ActivityManager.getActivity()).openGoodsPropertyFrag(new CarModelMainFragment());
                }
                break;
        }
    });

    public void setProperties(ArrayList<GoodsPropertyBean.GoodsProperty> properties) {
        mProperties = properties;
    }
}
